package selenium;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.*;

import java.util.List;
import java.util.concurrent.TimeUnit;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;

public class hometask3 {

    @DataProvider(name = "successTestData")
    public Object[][] createSuccessTestData() {
        return new Object[][] {
                { "userNameX", "userNameX@gamil.com","qwerty" }
        };
    }

    @DataProvider(name = "failedTestData")
    public Object[][] createFailedTestData() {
        return new Object[][] {
                { "userName1234", "userName1234@gamil.com","1234qwerty" },
                { "userName1234", "userName1234","1234qwerty" },
                { "userName1234", "userName1234@gamil.com","" }
        };
    }


    static WebDriver driver;

    @BeforeSuite
    public static void launchDriver() {

        driver = new FirefoxDriver();
    }

    @AfterSuite
    public static void closeDriver() {

        driver.quit();
    }

    // check whether we can register
    @Test(priority = 3,dataProvider = "successTestData" )
    public void registrationSuccess(String name, String email, String password) {
        System.out.println("Positive registration test started");


        driver.get("http://rozetka.com.ua/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.findElement(By.className("m-user-signin")).click();
        driver.findElement(By.className("auth-f-signup")).findElement(By.tagName("a")).click();
        System.out.println("Try to open registration page");

        // switch focus of WebDriver to the next found window handle (that's your newly opened window)
        for (String winHandle : driver.getWindowHandles()) {
            driver.switchTo().window(winHandle);
        }

        WebElement form = (new WebDriverWait(driver, 20))
                .until(ExpectedConditions.presenceOfElementLocated(By.className("iform")));
        form.findElement(By.cssSelector("input[name=title]")).sendKeys(name);
        form.findElement(By.cssSelector("input[name=email]")).sendKeys(email);
        form.findElement(By.cssSelector("input[name=password]")).sendKeys(password);
        form.findElement(By.cssSelector("button[type=submit]")).click();

        System.out.println("Filled in fields and submit a form");

        (new WebDriverWait(driver, 20))
                .until(ExpectedConditions.presenceOfElementLocated(By.className("profile-menu")));

        System.out.println("User's page is opened");
        String url = driver.getCurrentUrl();
        System.out.println(url);

        assertEquals("http://my.rozetka.com.ua/", url);

    }

    @Test(priority = 2, dataProvider = "failedTestData")
    public void registrationFailed(String name, String email, String password) {
        System.out.println("Negative registration test started");

        driver.get("http://rozetka.com.ua/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.findElement(By.className("m-user-signin")).click();
        driver.findElement(By.className("auth-f-signup")).findElement(By.tagName("a")).click();
        System.out.println("Try to open registration page");

        // switch focus of WebDriver to the next found window handle (that's your newly opened window)
        for (String winHandle : driver.getWindowHandles()) {
            driver.switchTo().window(winHandle);
        }

        WebElement form = (new WebDriverWait(driver, 20))
                .until(ExpectedConditions.presenceOfElementLocated(By.className("iform")));
        form.findElement(By.cssSelector("input[name=title]")).sendKeys(name);
        form.findElement(By.cssSelector("input[name=email]")).sendKeys(email);
        form.findElement(By.cssSelector("input[name=password]")).sendKeys(password);
        form.findElement(By.cssSelector("button[type=submit]")).click();
        System.out.println("Filled in fields and submit a form");


        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        // create an arraylist for checking whether 'user exists' message appeared
        List<WebElement> elems = driver.findElements(By.className("code4"));

        if (elems.size()>0) {
            System.out.println("Message 'this user already exists' appeared");
            assertFalse(false);
        }

        else {
            String url = driver.getCurrentUrl();
            System.out.println("User entered wrong credentials");
            assertEquals("http://my.rozetka.com.ua/signup/", url);
        }

    }


}
